package ysq.xyg.host.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import ysq.xyg.base.model.dto.ReturnEntity;
import ysq.xyg.base.service.impl.BaseServiceImpl;
import ysq.xyg.base.util.BaseConstant;
import ysq.xyg.host.dao.HostWatchDao;
import ysq.xyg.host.model.po.HostWatch;
import ysq.xyg.sys.model.po.SysUser;
import ysq.xyg.host.model.vo.HostWatchVo;
import ysq.xyg.host.service.HostWatchService;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;


/**
 * 房产模块-小区围观service实现
 *
 * @author congcong tao
 * @date 2019-8-30 10:34:41
 */
@Service("hostWatchService")
public class HostWatchServiceImpl extends BaseServiceImpl<HostWatch> implements HostWatchService {

    @Resource
    private HostWatchDao hostWatchDao;

    private HostWatch hostWatch;

    @Autowired
    public void setBaseDao(HostWatchDao hostWatchDao) {
        this.baseDao = hostWatchDao;
    }


    /**
     * 分页查询
     *
     * @param hostWatchVo 对象
     * @param pageSize 展示数量
     * @param pageNum 当前页面
     * @return ReturnEntity 对象
     * @author congcong tao
     * @date 2019-8-30 10:34:41
     */
    public ReturnEntity page(HostWatchVo hostWatchVo, int pageSize, int pageNum) {
        PageHelper.startPage(pageNum,pageSize);
        PageHelper.orderBy(" update_time desc");
        List<HostWatch> list = acceptList(hostWatchVo);
        PageInfo<HostWatch> page=new PageInfo<HostWatch>(list);
        return new ReturnEntity(page.getList(),page.getTotal(),page.isHasNextPage());
    }

    /**
     * 修改
     *
     * @param hostWatchVo 对象
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author congcong tao
     * @date 2019-8-30 10:34:41
     */
    public ReturnEntity addOrUpdate(HostWatchVo hostWatchVo, SysUser currentUser) {
        boolean isUpate=true;
        if(StrUtil.isEmpty(hostWatchVo.getId())){
            hostWatch=new HostWatch(currentUser);
            isUpate=false;
        }else{
            hostWatch=selectByPrimaryKey(hostWatchVo.getId());
            if(ObjectUtil.isNull(hostWatch)){
                return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
            }
            hostWatch.setUpdateTime(new Date());
            hostWatch.setUpdateUser(currentUser.getId());
        }
        hostWatch.setItemId(hostWatchVo.getItemId());
        hostWatch.setMemberId(hostWatchVo.getMemberId());
        int num=0;
        if(isUpate){
            num=updateByPrimaryKey(hostWatch);
        }else{
            num=insert(hostWatch);
        }
        if(num<=0){
            return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
        }
        return new ReturnEntity();
    }
    /**
     * 删除
     *
     * @param id 主键id
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author congcong tao
     * @date 2019-8-30 10:34:41
     */
    public ReturnEntity delete(String id, SysUser currentUser) {
        HostWatch hostWatch=selectByPrimaryKey(id);
        if(hostWatch!=null){
            hostWatch.setRemoveTag(BaseConstant.BASE_REMOVE_TAG_DELETE);
            hostWatch.setUpdateTime(new Date());
            hostWatch.setUpdateUser(currentUser.getId());
            int num=updateByPrimaryKey(hostWatch);
            if(num>0){
                return new ReturnEntity();
            }
        }
        return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
    }

    /**
     * 获取集合
     *
     * @param hostWatchVo 对象
     * @return List<BaseDict> BaseDict集合
     * @author congcong tao
     * @date 2019-8-30 10:34:41
     */
    public List<HostWatch> acceptList(HostWatchVo hostWatchVo){
        Example example = new Example(HostWatch.class);
        Example.Criteria criteria = example.createCriteria();
        if(StringUtil.isNotEmpty(hostWatchVo.getRemoveTag())){
            criteria.andEqualTo("removeTag",hostWatchVo.getRemoveTag());
        }
        example.setOrderByClause(" update_time desc");
        return hostWatchDao.selectByExample(example);
    }

    @Override
    public List<HostWatchVo> selectByItemId(String itemId) {
        return hostWatchDao.selectByItemId(itemId);
    }

    @Override
    public List<HostWatchVo> selectByItemIdAndSize(String itemId, int size) {
        PageHelper.startPage(0, size);
        List<HostWatchVo> hostWatchVos = hostWatchDao.selectByItemId(itemId);
        return hostWatchVos;
    }
}
